package com.chanven.lib.cptr.recyclerview.adapter;

import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuchangqing on 16/7/7.
 * RecyleView Base Adapter
 * http://blog.csdn.net/xiaozhiwz/article/details/46664719
 */
public abstract class BaseRecyleAdapterWithHf extends RecyclerView.Adapter implements AdapterInterface {

    protected final String TAG = getClass().getSimpleName();

    private final long DELAY_TIME = 1000;

    protected List<Object> mData;

    public abstract RecyclerView.ViewHolder ViewHolderCreate(ViewGroup parent, int viewType);

    public abstract void ViewHolderBind(RecyclerView.ViewHolder holder, int position);

    public BaseRecyleAdapterWithHf() {
        mData = new ArrayList<>();
        setHasStableIds(true);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return ViewHolderCreate(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //bind listener
        bindItemViewClickListener(holder, position);

        ViewHolderBind(holder, position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void setData(List<Object> data) {
        this.mData.clear();
        if (null == data || data.size() == 0) {
            return;
        }
        this.mData.addAll(data);
    }

    @Override
    public List<Object> getData() {
        return mData;
    }

    public Object getItem(int position) {
        if (null != getData() && position>=0 && position<getData().size()) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public void addData(Object data) {
        if (null == data) {
            return;
        }
        mData.add(data);
    }

    @Override
    public void addDataList(List<Object> data) {
        if (null == data || data.size() == 0) {
            return;
        }
        mData.addAll(data);
    }

    @Override
    public void clear() {
        mData.clear();
    }

    @Override
    public void updateChange() {
        this.notifyDataSetChanged();
    }

    /**
     * 局部刷新
     *
     * @param position
     */
    public void updateChange(int position) {
        this.notifyItemChanged(position);
    }

    @Override
    public void removeItem(int position) {
        if (position >= mData.size()) {
            return;
        }
        mData.remove(position);
    }

    /**
     * bind click listner to itemview
     */
    protected final void bindItemViewClickListener(RecyclerView.ViewHolder vh, final int position) {
        if (mOnItemClickListener != null) {
            vh.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setEnabledView(view, DELAY_TIME);
                    mOnItemClickListener.onItemClickListener(view, position);
                }
            });
        }
        if (mOnItemLongClickListener != null) {
            vh.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    setEnabledView(view, DELAY_TIME);
                    mOnItemLongClickListener.onItemLongClickListener(view, position);
                    return true;
                }
            });
        }
    }

    /**
     * click listener
     */
    public OnItemClickListener mOnItemClickListener;
    /**
     * long click listener
     */
    public OnItemLongClickListener mOnItemLongClickListener;

    /**
     * set a long click listener
     */
    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }

    /**
     * set a click listener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClickListener(View view, int position);
    }

    private void setEnabledView(final View v, long delayMillis) {
        if(v == null){
            return;
        }
        v.setEnabled(false);
        v.postDelayed(new Runnable() {
            @Override
            public void run() {
                v.setEnabled(true);
            }
        }, delayMillis);
    }
}